<script src="file://<%= ENV['TM_BUNDLE_SUPPORT'] %>/lib/shell.js" type='text/javascript'> </script>
<script src="file://<%= ENV['TM_BUNDLE_SUPPORT'] %>/lib/json.js" type='text/javascript'> </script>
<script>
var image_path = "file:///<%= ENV['TM_SUPPORT_PATH'] %>/images/";

function updateResult(html) {
    document.getElementById("result").innerHTML = html;
    sortables_init();
}

function getData(db, tbl, query, page_size, offset) {
    var res = shell_run("<%= ENV['TM_RUBY'] || 'ruby' %>", "<%= ENV['TM_BUNDLE_SUPPORT'] %>/bin/db_browser.rb", '--mode=frame', shell_join_long_args({database: db, table: tbl, query: query, rows: page_size, offset: offset}));
    updateResult(res);
}
function toggleDatabases(database, tbl, hname, stype, pw, dbuser, dbport, query, page_size, offset) {
    database_name = database.innerText;
    list = document.getElementById('db_' + database_name);
    if (list.getElementsByTagName('li').length == 0) {
        var res = shell_run("<%= ENV['TM_RUBY'] || 'ruby' %>", "<%= ENV['TM_BUNDLE_SUPPORT'] %>/bin/db_browser.rb", '--mode=tables', shell_join_long_args({database: database_name}));
        list.innerHTML = res;
        list.style.display = '';
    } else if (list.style.display != '') {
        list.style.display = '';
    } else {
        list.style.display = 'none';
    }
}

function doSearch(database, table, fields) {
    values = {};
    
    for (i = 0; i < fields.length; i++) {
        if (fields[i].type != 'search') continue;
        if (!(name = fields[i].name))   continue;
        if (!(value = fields[i].value)) continue;

        if (parseInt(value, 10)) {
            value = parseInt(value, 10);
        }
        values[name] = value;
    }

    commandString = shell_escape("<%= ENV['TM_BUNDLE_SUPPORT'] %>/bin/db_browser.rb") + 
                    " --mode=search " +
                    " --database=" + shell_escape(database) +
                    " --table=" + shell_escape(table);
    cmd = TextMate.system(commandString, function(cmd){
        updateResult(cmd.outputString);
    });
    cmd.write(values.toJSONString());
    cmd.close();
}

function launchConfig() {
    TextMate.system('"$TM_BUNDLE_SUPPORT"/bin/configure.sh 2>&1', null);
    window.close();
}
</script>
<script src="file://<%= ENV['TM_SUPPORT_PATH'] %>/script/sortable.js" type="text/javascript" charset="utf-8"></script>
